package top.chmin.project.cloud.sys.table.info;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import top.chmin.project.cloud.common.*;
import top.chmin.project.cloud.tool.LocalTool;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;



/**
 * chmin
 * 2024-01-25 14:19:37
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class TableInfo extends Model {
    /*###########auto#start###########*/
    // table前缀
    private String prefix;
    // table编号
    private String tableCode;
    // table注释
    private String tableComment;
    // table名称
    private String tableName;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public TableInfoModelCondition forInsert() {
        return new TableInfoModelCondition().forInsert();
    }

    public TableInfoModelCondition forUpdate() {
        return new TableInfoModelCondition().forUpdate();
    }

    public TableInfoModelCondition forDelete() {
        return new TableInfoModelCondition().forDelete();
    }

    public TableInfoModelCondition forSelect() {
        return new TableInfoModelCondition().forSelect();
    }

    public class TableInfoModelCondition extends Condition {
        TableInfoModelCondition parent = null;

        private TableInfoModelCondition() {
        }

        private TableInfoModelCondition(TableInfoModelCondition condition) {
            this.parent = condition;
        }

        private TableInfoModelCondition forInsert() {
            TableInfoModelCondition condition = new TableInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();

            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_table_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private TableInfoModelCondition forUpdate() {
            TableInfoModelCondition condition = new TableInfoModelCondition();
            /*###########auto#start###########*/
            if (prefix != null) {
                condition.getUpdateList().add("prefix = ?");
                condition.getUpdateValueList().add(prefix);
            }

            if (tableCode != null) {
                condition.getUpdateList().add("table_code = ?");
                condition.getUpdateValueList().add(tableCode);
            }

            if (tableComment != null) {
                condition.getUpdateList().add("table_comment = ?");
                condition.getUpdateValueList().add(tableComment);
            }

            if (tableName != null) {
                condition.getUpdateList().add("table_name = ?");
                condition.getUpdateValueList().add(tableName);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_table_info#update##where##order##limit#");
            return condition;
        }

        private TableInfoModelCondition forDelete() {
            TableInfoModelCondition condition = new TableInfoModelCondition();
            condition.setSql("DELETE FROM sys_table_info#where##group##order##limit#");
            return condition;
        }

        private TableInfoModelCondition forSelect() {
            TableInfoModelCondition condition = new TableInfoModelCondition();
            condition.modelClass = TableInfo.class;
            condition.setSql("SELECT #select# FROM sys_table_info#where##group##order##limit#");
            return condition;
        }

        public TableInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public TableInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public TableInfoModelCondition sub() {
            return new TableInfoModelCondition(this);
        }

        public TableInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public TableInfoCondition toFlex() {
            TableInfoCondition flex = new TableInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<TableInfoModelCondition> {
            private CreateBy() {
                super(TableInfoModelCondition.this, "create_by", TableInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<TableInfoModelCondition> {
            private CreateTime() {
                super(TableInfoModelCondition.this, "create_time", TableInfo.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<TableInfoModelCondition> {
            private Id() {
                super(TableInfoModelCondition.this, "id", TableInfo.this.getId());
            }
        }

        public Prefix prefix() {
            return new Prefix();
        }

        public class Prefix extends StringModelColumn<TableInfoModelCondition> {
            private Prefix() {
                super(TableInfoModelCondition.this, "prefix", TableInfo.this.getPrefix());
            }
        }

        public TableCode tableCode() {
            return new TableCode();
        }

        public class TableCode extends StringModelColumn<TableInfoModelCondition> {
            private TableCode() {
                super(TableInfoModelCondition.this, "table_code", TableInfo.this.getTableCode());
            }
        }

        public TableComment tableComment() {
            return new TableComment();
        }

        public class TableComment extends StringModelColumn<TableInfoModelCondition> {
            private TableComment() {
                super(TableInfoModelCondition.this, "table_comment", TableInfo.this.getTableComment());
            }
        }

        public TableName tableName() {
            return new TableName();
        }

        public class TableName extends StringModelColumn<TableInfoModelCondition> {
            private TableName() {
                super(TableInfoModelCondition.this, "table_name", TableInfo.this.getTableName());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<TableInfoModelCondition> {
            private UpdateBy() {
                super(TableInfoModelCondition.this, "update_by", TableInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<TableInfoModelCondition> {
            private UpdateTime() {
                super(TableInfoModelCondition.this, "update_time", TableInfo.this.getUpdateTime());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
